package v1

import (
	"ginTea/model"
	"ginTea/utils/errmsg"
	"ginTea/utils/helpers"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Request struct {
	UserID  int           `json:"user_id"`
	DataArr []interface{} `json:"data_arr"`
}

// 添加
func AddOrders(c *gin.Context) {
	var code int
	var data model.Orders
	_ = c.ShouldBindJSON(&data)
	code = model.CreateOrders(&data)

	response := helpers.Success("添加成功")
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"data":    response,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}

// // 删除订单
// func DeleteGoods(c *gin.Context) {
// 	id, _ := strconv.Atoi(c.Param("id"))
// 	code = model.DeleteGoods(id)
// 	if code == errmsg.ERROR_GOODS_NOT_EXIST {
// 		c.JSON(http.StatusOK, gin.H{
// 			"code":    code,
// 			"msg":     errmsg.GetErrMsg(code),
// 			"success": false,
// 		})
// 	} else {
// 		c.JSON(http.StatusOK, gin.H{
// 			"code": code,
// 			"msg":  errmsg.GetErrMsg(code),
// 		})
// 	}
// }

// // 获取订单列表
func GetOrdersList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	page, _ := strconv.Atoi(c.Query("page"))
	keyword := c.Query("keyword")
	Year, _ := strconv.Atoi(c.Query("year"))
	userId, _ := strconv.Atoi(c.Query("id"))
	if pageSize == 0 {
		pageSize = 10
	}
	if page == 0 {
		page = 1
	}

	data := model.GetOrdersList(page, pageSize, keyword, userId, Year)
	c.JSON(http.StatusOK, data)
}

// // 编辑订单
func UpdateOrderStatus(c *gin.Context) {
	var code int
	var data model.Orders
	id, _ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.UpdateOrderStatus(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"success": true,
	})
}
